﻿using System;

namespace SimpleEvent
{
    /***********发布器类***********/
    public class EventTest
    {
        // 声明委托
        public delegate void NumManipulationHandler();
        // 声明事件
        public event NumManipulationHandler ChangeNum;

        public void OpenDoor()
        {
            // 事件触发
            ChangeNum();
        }
    }

    /***********订阅器类***********/
    public class SubscribEvent
    {
        public void Printf()
        {
            Console.WriteLine("The door is opened.");
        }
    }

    /***********触发***********/
    public class MainClass
    {
        public static void Main()
        {
            // 实例化事件触发对象
            EventTest e = new EventTest();
            // 实例化订阅事件对象
            SubscribEvent v = new SubscribEvent();

            /* 订阅器的Printf()在事件触发对象中注册到委托事件中 */
            e.ChangeNum += new EventTest.NumManipulationHandler(v.Printf);
            /* 触发了事件 */
            e.OpenDoor();

            Console.ReadKey();
        }
    }
}